home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / gdb / gdb_18s.zoo / source.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-27  |  23.7 KB  |  935 lines

  1. /* List lines of source files for GDB, the GNU debugger.
  2.    Copyright (C) 1986, 1987, 1988 Free Software Foundation, Inc.
  3.  
  4. GDB is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the GDB General Public License for full details.
  9.  
  10. Everyone is granted permission to copy, modify and redistribute GDB,
  11. but only under the conditions described in the GDB General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with GDB so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.  
  17. In other words, go ahead and share GDB, but don't try to stop
  18. anyone else from sharing it farther.  Help stamp out software hoarding!
  19. */
  20.  
  21. #include <stdio.h>
  22.  
  23. #ifdef atarist
  24. #include <param.h>
  25. #include <types.h>
  26. #include <stat.h>
  27. #include <file.h>
  28. #else
  29. #include <sys/param.h>
  30. #include <sys/stat.h>
  31. #include <sys/file.h>
  32. #endif
  33.  
  34. #include "defs.h"
  35. #include "symtab.h"
  36.  
  37. /* Path of directories to search for source files.
  38.    Same format as the PATH environment variable's value.  */
  39.  
  40. static char *source_path;
  41.  
  42. /* Symtab of default file for listing lines of.  */
  43.  
  44. struct symtab *current_source_symtab;
  45.  
  46. /* Default next line to list.  */
  47.  
  48. int current_source_line;
  49.  
  50. /* Line number of last line printed.  Default for various commands.
  51.    current_source_line is usually, but not always, the same as this.  */
  52.  
  53. static int last_line_listed;
  54.  
  55. /* First line number listed by last listing command.  */
  56.  
  57. static int first_line_listed;
  58.  
  59.  
  60. /* Set the source file default for the "list" command,
  61.    specifying a symtab.  */
  62.  
  63. void
  64. select_source_symtab (s)
  65.      register struct symtab *s;
  66. {
  67.   struct symtab_and_line sal;
  68.   if (s)
  69.   {
  70.       current_source_symtab = s;
  71.       current_source_line = 1;
  72.       return;
  73.   }
  74.   /* Make the default place to list be the function `main'
  75.      if one exists.  */
  76.   if (lookup_symbol ("main", 0, VAR_NAMESPACE))
  77.   {
  78.       sal = decode_line_spec ("main", 1);
  79.       current_source_symtab = sal.symtab;
  80.       current_source_line = max (sal.line - 9, 1);
  81.       return;
  82.   }
  83.  
  84.       /* If there is no `main', use the last symtab in the list,
  85.      which is actually the first found in the file's symbol table.
  86.      But ignore .h files.  */
  87.   if (s = symtab_list)
  88.   {
  89.       do
  90.       {
  91.       char *name = s->filename;
  92.       int len = strlen (name);
  93.       if (! (len > 2 && !strcmp (&name[len - 2], ".h")))
  94.         current_source_symtab = s;
  95.       s = s->next;
  96.       }
  97.       while (s);
  98.       current_source_line = 1;
  99.   }
  100. }
  101.  
  102. static void
  103. directories_info ()
  104. {
  105.  printf_filtered ("Source directories searched: %s\n", source_path);
  106. }
  107.  
  108. void
  109. init_source_path ()
  110. {
  111.   register struct symtab *s;
  112.  
  113.   source_path = savestring (current_directory, strlen (current_directory));
  114.  
  115.   /* Forget what we learned about line positions in source files;
  116.      must check again now since files may be found in
  117.      a different directory now.  */
  118.   for (s = symtab_list; s; s = s->next)
  119.     if (s->line_charpos != 0)
  120.       {
  121.     free (s->line_charpos);
  122.     s->line_charpos = 0;
  123.       }
  124. }
  125.  
  126. void
  127. directory_command (dirname, from_tty)
  128.      char *dirname;
  129.      int from_tty;
  130. {
  131.   char *old = source_path;
  132.  
  133.   if (dirname == 0)
  134.     {
  135.       if (query ("Reinitialize source path to %s? ", current_directory))
  136.     {
  137.       init_source_path ();
  138.       free (old);
  139.     }
  140.     }
  141.   else
  142.     {
  143.       struct stat st;
  144.       register int len = strlen (dirname);
  145.       register char *tem;
  146.       extern char *index ();
  147.  
  148. #ifdef atarist
  149.       if (index (dirname, ','))
  150. #else
  151.       if (index (dirname, ':'))
  152. #endif
  153.     error ("Please add one directory at a time to the source path.");
  154. #ifdef atarist
  155.       if ((dirname[len - 1] == '\\') || (dirname[len - 1] == '/'))
  156. #else
  157.       if (dirname[len - 1] == '/')
  158. #endif
  159.       /* Sigh. "foo/" => "foo" */
  160.       dirname[--len] == '\0';
  161.  
  162.       while (dirname[len - 1] == '.')
  163.     {
  164.       if (len == 1)
  165.         {
  166.           /* "." => getwd () */
  167.           dirname = current_directory;
  168.           goto append;
  169.         }
  170. #ifdef atarist
  171.       else if ((dirname[len - 2] == '\\') || (dirname[len - 2] == '/'))
  172. #else
  173.       else if (dirname[len - 2] == '/')
  174. #endif
  175.         {
  176.           if (len == 2)
  177.         {
  178.           /* "/." => "/" */
  179.           dirname[--len] = '\0';
  180.           goto append;
  181.         }
  182.           else
  183.         {
  184.           /* "...foo/." => "...foo" */
  185.           dirname[len -= 2] = '\0';
  186.           continue;
  187.         }
  188.         }
  189.       break;
  190.     }
  191.  
  192. #ifdef atarist
  193.       if ((dirname[0] != '\\') && (dirname[0] != '/') && (dirname[1] != ':'))
  194.     dirname = concat (current_directory, "\\", dirname);
  195. #else
  196.       if (dirname[0] != '/')
  197.     dirname = concat (current_directory, "/", dirname);
  198. #endif
  199.       else
  200.     dirname = savestring (dirname, len);
  201.       make_cleanup (free, dirname);
  202.  
  203.       if (stat (dirname, &st) < 0)
  204.     perror_with_name (dirname);
  205.       if ((st.st_mode & S_IFMT) != S_IFDIR)
  206.     error ("%s is not a directory.", dirname);
  207.  
  208.     append:
  209.       len = strlen (dirname);
  210.       tem = source_path;
  211.       while (1)
  212.     {
  213.       if (!strncmp (tem, dirname, len)
  214. #ifdef atarist
  215.           && (tem[len] == '\0' || tem[len] == ','))
  216. #else
  217.           && (tem[len] == '\0' || tem[len] == ':'))
  218. #endif
  219.         {
  220.          printf_filtered ("\"%s\" is already in the source path.\n",
  221.               dirname);
  222.           break;
  223.         }
  224. #ifdef atarist
  225.       tem = index (tem, ',');
  226. #else
  227.       tem = index (tem, ':');
  228. #endif
  229.       if (tem)
  230.         tem++;
  231.       else
  232.         {
  233. #ifdef atarist
  234.           source_path = concat (old, ",", dirname);
  235. #else
  236.           source_path = concat (old, ":", dirname);
  237. #endif
  238.           free (old);
  239.           break;
  240.         }
  241.     }
  242.       if (from_tty)
  243.     directories_info ();
  244.     }
  245. }
  246.  
  247. /* Open a file named STRING, searching path PATH (dir names sep by colons)
  248.    using mode MODE and protection bits PROT in the calls to open.
  249.    If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
  250.    (ie pretend the first element of PATH is ".")
  251.    If FILENAMED_OPENED is non-null, set it to a newly allocated string naming
  252.    the actual file opened (this string will always start with a "/"
  253.  
  254.    If a file is found, return the descriptor.
  255.    Otherwise, return -1, with errno set for the last name we tried to open.  */
  256.  
  257. /*  >>>> This should only allow files of certain types,
  258.     >>>>  eg executable, non-directory */
  259. int
  260. openp (path, try_cwd_first, string, mode, prot, filename_opened)
  261.      char *path;
  262.      int try_cwd_first;
  263.      char *string;
  264.      int mode;
  265.      int prot;
  266.      char **filename_opened;
  267. {
  268.   register int fd;
  269.   register char *filename;
  270.   register char *p, *p1;
  271.   register int len;
  272.  
  273.   if (!path)
  274.     path = ".";
  275.  
  276.   /* ./foo => foo */
  277. #ifdef atarist
  278.   while (string[0] == '.' && ((string[1] == '\\') || (string[1] == '/')))
  279. #else
  280.   while (string[0] == '.' && string[1] == '/')
  281. #endif
  282.     string += 2;
  283.  
  284. #ifdef atarist
  285.   if (try_cwd_first || ((string[0] == '\\') || (string[0] == '/')))
  286. #else
  287.   if (try_cwd_first || string[0] == '/')
  288. #endif
  289.     {
  290.       filename = string;
  291.       fd = open (filename, mode, prot);
  292. #ifdef atarist
  293.       if (fd >= 0 || ((string[0] == '\\') || (string[0] == '/')))
  294. #else
  295.       if (fd >= 0 || string[0] == '/')
  296. #endif
  297.     goto done;
  298.     }
  299.  
  300.   filename = (char *) alloca (strlen (path) + strlen (string) + 2);
  301.   fd = -1;
  302.   for (p = path; p; p = p1 ? p1 + 1 : 0)
  303.     {
  304. #ifdef atarist
  305.       p1 = (char *) index (p, ',');
  306. #else
  307.       p1 = (char *) index (p, ':');
  308. #endif
  309.       if (p1)
  310.     len = p1 - p;
  311.       else
  312.     len = strlen (p);
  313.  
  314.       strncpy (filename, p, len);
  315.       filename[len] = 0;
  316. #ifdef atarist
  317.       strcat (filename, "\\");
  318. #else
  319.       strcat (filename, "/");
  320. #endif
  321.       strcat (filename, string);
  322.  
  323.       fd = open (filename, mode, prot);
  324.       if (fd >= 0) break;
  325.     }
  326.  
  327.  done:
  328.   if (filename_opened)
  329.     if (fd < 0)
  330.       *filename_opened = (char *) 0;
  331. #ifdef atarist
  332.     else if (((filename[0] == '\\') || (filename[0] == '/')) || (filename[1] == ':'))
  333. #else
  334.     else if (filename[0] == '/')
  335. #endif
  336.       *filename_opened = savestring (filename, strlen (filename));
  337.     else
  338.       {
  339. #ifdef atarist
  340.     *filename_opened = concat (current_directory, "\\", filename);
  341. #else
  342.     *filename_opened = concat (current_directory, "/", filename);
  343. #endif
  344.       }
  345.  
  346.   return fd;
  347. }
  348.  
  349. /* Create and initialize the table S->line_charpos that records
  350.    the positions of the lines in the source file, which is assumed
  351.    to be open on descriptor DESC.
  352.    All set S->nlines to the number of such lines.  */
  353.  
  354. static void
  355. find_source_lines (s, desc)
  356.      struct symtab *s;
  357.      int desc;
  358. {
  359.   struct stat st;
  360.   register char *data, *p, *end;
  361.   int nlines = 0;
  362.   int lines_allocated = 1000;
  363.   int *line_charpos = (int *) xmalloc (lines_allocated * sizeof (int));
  364.   extern int exec_mtime;
  365.  
  366. #ifdef atarist
  367.   stat (s->fullname, &st);
  368. #else
  369.   fstat (desc, &st);
  370.   if (get_exec_file (0) != 0 && exec_mtime < st.st_mtime)
  371.    printf_filtered ("Source file is more recent than executable.\n");
  372. #endif
  373.  
  374.   data = (char *) alloca (st.st_size);
  375.   if (myread (desc, data, st.st_size) < 0)
  376.     perror_with_name (s->filename);
  377.   end = data + st.st_size;
  378.   p = data;
  379.   line_charpos[0] = 0;
  380.   nlines = 1;
  381.   while (p != end)
  382.     {
  383.       if (*p++ == '\n'
  384.       /* A newline at the end does not start a new line.  */
  385.       && p != end)
  386.     {
  387.       if (nlines == lines_allocated)
  388.         line_charpos = (int *) xrealloc (line_charpos,
  389.                          sizeof (int) * (lines_allocated *= 2));
  390.       line_charpos[nlines++] = p - data;
  391.     }
  392.     }
  393.   s->nlines = nlines;
  394.   s->line_charpos = (int *) xrealloc (line_charpos, nlines * sizeof (int));
  395. }
  396.  
  397. /* Return the character position of a line LINE in symtab S.
  398.    Return 0 if anything is invalid.  */
  399.  
  400. int
  401. source_line_charpos (s, line)
  402.      struct symtab *s;
  403.      int line;
  404. {
  405.   if (!s) return 0;
  406.   if (!s->line_charpos || line <= 0) return 0;
  407.   if (line > s->nlines)
  408.     line = s->nlines;
  409.   return s->line_charpos[line - 1];
  410. }
  411.  
  412. /* Return the line number of character position POS in symtab S.  */
  413.  
  414. int
  415. source_charpos_line (s, chr)
  416.     register struct symtab *s;
  417.     register int chr;
  418. {
  419.   register int line = 0;
  420.   register int *lnp;
  421.     
  422.   if (s == 0 || s->line_charpos == 0) return 0;
  423.   lnp = s->line_charpos;
  424.   /* Files are usually short, so sequential search is Ok */
  425.   while (line < s->nlines  && *lnp <= chr)
  426.     {
  427.       line++;
  428.       lnp++;
  429.     }
  430.   if (line >= s->nlines)
  431.     line = s->nlines;
  432.   return line;
  433. }
  434.  
  435. /* Get full pathname and line number positions for a symtab.
  436.    Return nonzero if line numbers may have changed.
  437.    Set *FULLNAME to actual name of the file as found by `openp',
  438.    or to 0 if the file is not found.  */
  439.  
  440. int
  441. get_filename_and_charpos (s, line, fullname)
  442.      struct symtab *s;
  443.      int line;
  444.      char **fullname;
  445. {
  446.   register int desc, linenums_changed = 0;
  447.   
  448.   desc = openp (source_path, 0, s->filename, O_RDONLY, 0, &s->fullname);
  449.   if (desc < 0)
  450.     {
  451.       if (fullname)
  452.     *fullname = NULL;
  453.       return 0;
  454.     }  
  455.   if (fullname)
  456.     *fullname = s->fullname;
  457.   if (s->line_charpos == 0) linenums_changed = 1;
  458.   if (linenums_changed) find_source_lines (s, desc);
  459.   close (desc);
  460.   return linenums_changed;
  461. }
  462.  
  463. /* Print text describing the full name of the source file S
  464.    and the line number LINE and its corresponding character position.
  465.    The text starts with two Ctrl-z so that the Emacs-GDB interface
  466.    can easily find it.
  467.  
  468.    MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
  469.  
  470.    Return 1 if successful, 0 if could not find the file.  */
  471.  
  472. int
  473. identify_source_line (s, line, mid_statement)
  474.      struct symtab *s;
  475.      int line;
  476.      int mid_statement;
  477. {
  478.   if (s->line_charpos == 0)
  479.     get_filename_and_charpos (s, line, 0);
  480.   if (s->fullname == 0)
  481.     return 0;
  482.  printf_filtered ("\032\032%s:%d:%d:%s\n", s->fullname,
  483.       line, s->line_charpos[line - 1],
  484.       mid_statement ? "middle" : "beg");
  485.   current_source_line = line;
  486.   first_line_listed = line;
  487.   last_line_listed = line;
  488.   current_source_symtab = s;
  489.   return 1;
  490. }
  491.  
  492. /* Print source lines from the file of symtab S,
  493.    starting with line number LINE and stopping before line number STOPLINE.  */
  494.  
  495. void
  496. print_source_lines (s, line, stopline)
  497.      struct symtab *s;
  498.      int line, stopline;
  499. {
  500.   register int c;
  501.   register int desc;
  502.   register FILE *stream;
  503.   int nlines = stopline - line;
  504.  
  505.   desc = openp (source_path, 0, s->filename, O_RDONLY, 0, &s->fullname);
  506.   if (desc < 0)
  507.     perror_with_name (s->filename);
  508.  
  509.   if (s->line_charpos == 0)
  510.     find_source_lines (s, desc);
  511.  
  512.   if (line < 1 || line > s->nlines)
  513.     {
  514.       close (desc);
  515.       error ("Line number %d out of range; %s has %d lines.",
  516.          line, s->filename, s->nlines);
  517.     }
  518.  
  519.   if (lseek (desc, s->line_charpos[line - 1], 0) < 0)
  520.     {
  521.       close (desc);
  522.       perror_with_name (s->filename);
  523.     }
  524.  
  525.   current_source_symtab = s;
  526.   current_source_line = line;
  527.   first_line_listed = line;
  528.   
  529.   stream = fdopen (desc, "rt");
  530.   clearerr (stream);
  531.  
  532.   while (nlines-- > 0)
  533.     {
  534.       c = fgetc (stream);
  535.       if (c == EOF) break;
  536.       last_line_listed = current_source_line;
  537.      printf_filtered ("%d\t", current_source_line++);
  538.       do
  539. /*
  540. #ifdef atarist
  541.     if (c != '\r')
  542. #endif
  543. */
  544.     {
  545.       if (c < 040 && c != '\t' && c != '\n')
  546.         {
  547.           fputc_filtered ('^', stdout);
  548.           fputc_filtered (c + 0100, stdout);
  549.         }
  550.       else if (c == 0177)
  551.        printf_filtered ("^?");
  552.       else
  553.         fputc_filtered (c, stdout);
  554.     } while (c != '\n' && (c = fgetc (stream)) >= 0);
  555.     }
  556.  
  557.   fclose (stream);
  558. }
  559.  
  560. static void
  561. list_command (arg, from_tty)
  562.      char *arg;
  563.      int from_tty;
  564. {
  565.   struct symtab_and_line sal, sal_end;
  566.   struct symbol *sym;
  567.   char *arg1;
  568.   int no_end = 1;
  569.   int dummy_end = 0;
  570.   int dummy_beg = 0;
  571.   int linenum_beg = 0;
  572.   char *p;
  573.  
  574.   if (symtab_list == 0)
  575.     error ("No symbol table is loaded. Listing source lines requires symbols.");
  576.  
  577.   /* Pull in a current source symtab if necessary */
  578.   if (current_source_symtab == 0 &&
  579.       (arg == 0 || arg[0] == '+' || arg[0] == '-'))
  580.     select_source_symtab (0);
  581.  
  582.   /* "l" or "l +" lists next ten lines.  */
  583.  
  584.   if (arg == 0 || !strcmp (arg, "+"))
  585.     {
  586.       if (current_source_symtab == 0)
  587.     error ("No default source file yet.  Do \"help list\".");
  588.       print_source_lines (current_source_symtab, current_source_line,
  589.               current_source_line + 10);
  590.       return;
  591.     }
  592.  
  593.   /* "l -" lists previous ten lines, the ones before the ten just listed.  */
  594.   if (!strcmp (arg, "-"))
  595.     {
  596.       if (current_source_symtab == 0)
  597.     error ("No default source file yet.  Do \"help list\".");
  598.       print_source_lines (current_source_symtab,
  599.               max (first_line_listed - 10, 1),
  600.               first_line_listed);
  601.       return;
  602.     }
  603.  
  604.   /* Now if there is only one argument, decode it in SAL
  605.      and set NO_END.
  606.      If there are two arguments, decode them in SAL and SAL_END
  607.      and clear NO_END; however, if one of the arguments is blank,
  608.      set DUMMY_BEG or DUMMY_END to record that fact.  */
  609.  
  610.   arg1 = arg;
  611.   if (*arg1 == ',')
  612.     dummy_beg = 1;
  613.   else
  614.     sal = decode_line_1 (&arg1, 0, 0, 0);
  615.  
  616.   /* Record whether the BEG arg is all digits.  */
  617.  
  618.   for (p = arg; p != arg1 && *p >= '0' && *p <= '9'; p++);
  619.   linenum_beg = (p == arg1);
  620.  
  621.   while (*arg1 == ' ' || *arg1 == '\t')
  622.     arg1++;
  623.   if (*arg1 == ',')
  624.     {
  625.       no_end = 0;
  626.       arg1++;
  627.       while (*arg1 == ' ' || *arg1 == '\t')
  628.     arg1++;
  629.       if (*arg1 == 0)
  630.     dummy_end = 1;
  631.       else if (dummy_beg)
  632.     sal_end = decode_line_1 (&arg1, 0, 0, 0);
  633.       else
  634.     sal_end = decode_line_1 (&arg1, 0, sal.symtab, sal.line);
  635.     }
  636.  
  637.   if (*arg1)
  638.     error ("Junk at end of line specification.");
  639.  
  640.   if (!no_end && !dummy_beg && !dummy_end
  641.       && sal.symtab != sal_end.symtab)
  642.     error ("Specified start and end are in different files.");
  643.   if (dummy_beg && dummy_end)
  644.     error ("Two empty args do not say what lines to list.");
  645.  
  646.   /* if line was specified by address,
  647.      first print exactly which line, and which file.
  648.      In this case, sal.symtab == 0 means address is outside
  649.      of all known source files, not that user failed to give a filename.  */
  650.   if (*arg == '*')
  651.     {
  652.       if (sal.symtab == 0)
  653.     error ("No source file for address 0x%x.", sal.pc);
  654.       sym = find_pc_function (sal.pc);
  655.       if (sym)
  656. printf_filtered ("0x%x is in %s (%s, line %d).\n",
  657.         sal.pc, SYMBOL_NAME (sym), sal.symtab->filename, sal.line);
  658.       else
  659. printf_filtered ("0x%x is in %s, line %d.\n",
  660.         sal.pc, sal.symtab->filename, sal.line);
  661.     }
  662.  
  663.   /* If line was not specified by just a line number,
  664.      and it does not imply a symtab, it must be an undebuggable symbol
  665.      which means no source code.  */
  666.  
  667.   if (! linenum_beg && sal.symtab == 0)
  668.     error ("No line number known for %s.", arg);
  669.  
  670.   /* If this command is repeated with RET,
  671.      turn it into the no-arg variant.  */
  672.  
  673.   if (from_tty)
  674.     *arg = 0;
  675.  
  676.   if (dummy_beg && sal_end.symtab == 0)
  677.     error ("No default source file yet.  Do \"help list\".");
  678.   if (dummy_beg)
  679.     print_source_lines (sal_end.symtab, max (sal_end.line - 9, 1),
  680.             sal_end.line + 1);
  681.   else if (sal.symtab == 0)
  682.     error ("No default source file yet.  Do \"help list\".");
  683.   else if (no_end)
  684.     print_source_lines (sal.symtab, max (sal.line - 5, 1), sal.line + 5);
  685.   else
  686.     print_source_lines (sal.symtab, sal.line,
  687.             dummy_end ? sal.line + 10 : sal_end.line + 1);
  688. }
  689.  
  690. /* Print info on range of pc's in a specified line.  */
  691.  
  692. static void
  693. line_info (arg, from_tty)
  694.      char *arg;
  695.      int from_tty;
  696. {
  697.   struct symtab_and_line sal;
  698.   int start_pc, end_pc;
  699.  
  700.   if (arg == 0)
  701.     {
  702.       sal.symtab = current_source_symtab;
  703.       sal.line = last_line_listed;
  704.     }
  705.   else
  706.     {
  707.       sal = decode_line_spec (arg, 0);
  708.  
  709.       /* If this command is repeated with RET,
  710.      turn it into the no-arg variant.  */
  711.  
  712.       if (from_tty)
  713.     *arg = 0;
  714.     }
  715.  
  716.   if (sal.symtab == 0)
  717.     error ("No source file specified.");
  718.   if (sal.line > 0
  719.       && find_line_pc_range (sal.symtab, sal.line, &start_pc, &end_pc))
  720.     {
  721.       if (start_pc == end_pc)
  722. printf_filtered ("Line %d of \"%s\" is at pc 0x%x but contains no code.\n",
  723.         sal.line, sal.symtab->filename, start_pc);
  724.       else
  725. printf_filtered ("Line %d of \"%s\" starts at pc 0x%x and ends at 0x%x.\n",
  726.         sal.line, sal.symtab->filename, start_pc, end_pc);
  727.       /* x/i should display this line's code.  */
  728.       set_next_address (start_pc);
  729.       /* Repeating "info line" should do the following line.  */
  730.       last_line_listed = sal.line + 1;
  731.     }
  732.   else
  733.    printf_filtered ("Line number %d is out of range for \"%s\".\n",
  734.         sal.line, sal.symtab->filename);
  735. }
  736.  
  737. /* Commands to search the source file for a regexp.  */
  738.  
  739. static void
  740. forward_search_command (regex, from_tty)
  741.      char *regex;
  742. {
  743.   register int c;
  744.   register int desc;
  745.   register FILE *stream;
  746.   int line = last_line_listed + 1;
  747.   char *msg;
  748.  
  749.   msg = (char *) re_comp (regex);
  750.   if (msg)
  751.     error (msg);
  752.  
  753.   if (current_source_symtab == 0) 
  754.       select_source_symtab (0);
  755. /* WAS:    error ("No default source file yet.  Do \"help list\"."); */
  756.  
  757.   /* Search from last_line_listed+1 in current_source_symtab */
  758.  
  759.   desc = openp (source_path, 0, current_source_symtab->filename,
  760.         O_RDONLY, 0, ¤t_source_symtab->fullname);
  761.   if (desc < 0)
  762.     perror_with_name (current_source_symtab->filename);
  763.  
  764.   if (current_source_symtab->line_charpos == 0)
  765.     find_source_lines (current_source_symtab, desc);
  766.  
  767.   if (line < 1 || line >= current_source_symtab->nlines)
  768.     {
  769.       close (desc);
  770.       error ("Expression not found");
  771.     }
  772.  
  773.   if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
  774.     {
  775.       close (desc);
  776.       perror_with_name (current_source_symtab->filename);
  777.     }
  778.  
  779.   stream = fdopen (desc, "rt");
  780.   clearerr (stream);
  781.   while (1) {
  782.     char buf[4096];        /* Should be reasonable??? */
  783.     register char *p = buf;
  784.  
  785.     c = fgetc (stream);
  786.     if (c == EOF)
  787.       break;
  788.     do {
  789.       *p++ = c;
  790.     } while (c != '\n' && (c = fgetc (stream)) >= 0);
  791.  
  792.     /* we now have a source line in buf, null terminate and match */
  793.     *p = 0;
  794.     if (re_exec (buf) > 0)
  795.       {
  796.     /* Match! */
  797.     fclose (stream);
  798.     print_source_lines (current_source_symtab,
  799.                line, line+1);
  800.     current_source_line = max (line - 5, 1);
  801.     return;
  802.       }
  803.     line++;
  804.   }
  805.  
  806.  printf_filtered ("Expression not found\n");
  807.   fclose (stream);
  808. }
  809.  
  810. static void
  811. reverse_search_command (regex, from_tty)
  812.      char *regex;
  813. {
  814.   register int c;
  815.   register int desc;
  816.   register FILE *stream;
  817.   int line = last_line_listed - 1;
  818.   char *msg;
  819.  
  820.   msg = (char *) re_comp (regex);
  821.   if (msg)
  822.     error (msg);
  823.  
  824.   if (current_source_symtab == 0) 
  825.       select_source_symtab (0);
  826. /* WAS:     error ("No default source file yet.  Do \"help list\"."); */
  827.  
  828.   /* Search from last_line_listed-1 in current_source_symtab */
  829.  
  830.   desc = openp (source_path, 0, current_source_symtab->filename,
  831.         O_RDONLY, 0, ¤t_source_symtab->fullname);
  832.   if (desc < 0)
  833.     perror_with_name (current_source_symtab->filename);
  834.  
  835.   if (current_source_symtab->line_charpos == 0)
  836.     find_source_lines (current_source_symtab, desc);
  837.  
  838.   if (line < 1 || line >= current_source_symtab->nlines)
  839.     {
  840.       close (desc);
  841.       error ("Expression not found");
  842.     }
  843.  
  844.   if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
  845.     {
  846.       close (desc);
  847.       perror_with_name (current_source_symtab->filename);
  848.     }
  849.  
  850.   stream = fdopen (desc, "rt");
  851.   clearerr (stream);
  852.   while (1)
  853.     {
  854.       char buf[4096];        /* Should be reasonable??? */
  855.       register char *p = buf;
  856.  
  857.       c = fgetc (stream);
  858.       if (c == EOF)
  859.     break;
  860.       do {
  861.     *p++ = c;
  862.       } while (c != '\n' && (c = fgetc (stream)) >= 0);
  863.  
  864.       /* We now have a source line in buf; null terminate and match.  */
  865.       *p = 0;
  866.       if (re_exec (buf) > 0)
  867.     {
  868.       /* Match! */
  869.       fclose (stream);
  870.       print_source_lines (current_source_symtab,
  871.                   line, line+1);
  872.       current_source_line = max (line - 5, 1);
  873.       return;
  874.     }
  875.       line--;
  876.       if (fseek (stream, current_source_symtab->line_charpos[line - 1], 0) < 0)
  877.     {
  878.       fclose (stream);
  879.       perror_with_name (current_source_symtab->filename);
  880.     }
  881.     }
  882.  
  883.  printf_filtered ("Expression not found\n");
  884.   fclose (stream);
  885.   return;
  886. }
  887.  
  888. void
  889. initialize_source ()
  890. {
  891.   current_source_symtab = 0;
  892.   init_source_path ();
  893.  
  894.   add_com ("directory", class_files, directory_command,
  895.        "Add directory DIR to end of search path for source files.\n\
  896. With no argument, reset the search path to just the working directory\n\
  897. and forget cached info on line positions in source files.");
  898.  
  899.   add_info ("directories", directories_info,
  900.         "Current search path for finding source files.");
  901.  
  902.   add_info ("line", line_info,
  903.         "Core addresses of the code for a source line.\n\
  904. Line can be specified as\n\
  905.   LINENUM, to list around that line in current file,\n\
  906.   FILE:LINENUM, to list around that line in that file,\n\
  907.   FUNCTION, to list around beginning of that function,\n\
  908.   FILE:FUNCTION, to distinguish among like-named static functions.\n\
  909. Default is to describe the last source line that was listed.\n\n\
  910. This sets the default address for \"x\" to the line's first instruction\n\
  911. so that \"x/i\" suffices to start examining the machine code.\n\
  912. The address is also stored as the value of \"$_\".");
  913.  
  914.   add_com ("forward-search", class_files, forward_search_command,
  915.        "Search for regular expression (see regex(3)) from last line listed.");
  916.   add_com_alias ("search", "forward-search", class_files, 0);
  917.  
  918.   add_com ("reverse-search", class_files, reverse_search_command,
  919.        "Search backward for regular expression (see regex(3)) from last line listed.");
  920.  
  921.   add_com ("list", class_files, list_command,
  922.        "List specified function or line.\n\
  923. With no argument, lists ten more lines after or around previous listing.\n\
  924. \"list -\" lists the ten lines before a previous ten-line listing.\n\
  925. One argument specifies a line, and ten lines are listed around that line.\n\
  926. Two arguments with comma between specify starting and ending lines to list.\n\
  927. Lines can be specified in these ways:\n\
  928.   LINENUM, to list around that line in current file,\n\
  929.   FILE:LINENUM, to list around that line in that file,\n\
  930.   FUNCTION, to list around beginning of that function,\n\
  931.   FILE:FUNCTION, to distinguish among like-named static functions.\n\
  932.   *ADDRESS, to list around the line containing that address.\n\
  933. With two args if one is empty it stands for ten lines away from the other arg.");
  934. }
  935.